import { L2EProtocol, S2CProtocol, TIP_INFO } from "../proto/ELProtocol";
import { dailyAdCode } from "../utils/CommonUtils";
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { NotificationCenter } from "../utils/NotificationCenter";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    private prizeDay: number;
    private hasTake: number;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        if (cc.sys.isNative) {
            jsb.reflection.callStaticMethod("com/bbl/pineapple/JniHelper", "showBannerAd", "()V");
        }
        this.node.on(cc.Node.EventType.TOUCH_END, this.checkClose, this);
        NotificationCenter.Instance.on(S2CProtocol.s2c_7day_info, this.setupView, this);
        NotificationCenter.Instance.on(L2EProtocol.l2e_7day_pri, this.takePrize, this);
    }

    onDestroy() {
        if (cc.sys.isNative) {
            jsb.reflection.callStaticMethod("com/bbl/pineapple/JniHelper", "hideBannerAd", "()V");
        }
        this.node.off(cc.Node.EventType.TOUCH_END, this.checkClose, this);
        NotificationCenter.Instance.off(S2CProtocol.s2c_7day_info, this.setupView, this);
        NotificationCenter.Instance.off(L2EProtocol.l2e_7day_pri, this.takePrize, this);
    }

    start () {
        NotificationCenter.Instance.emit("request7Day");
    }

    // update (dt) {}

    
    public setupView(data: any) {
        this.prizeDay = data.prizeDay;
        this.hasTake = data.hasTake;

        for (let index = 0; index < 7; index++) {
            let btn_reward = cc.find('im_bg/btn_reward'+(index+1), this.node);
            // if (index < 6) {
            //     btn_reward.getChildByName('label_index').getComponent(cc.Label).string = "第"+(index+1)+"天";
            // }

            // let imLight = btn_reward.getChildByName('im_light');
            let im_flag_take = btn_reward.getChildByName('im_flag_take');

            // if (index+1!=7) {
                let im_mask = btn_reward.getChildByName('im_mask');
                im_mask.active = (index+1 < data.prizeDay);
                
                if (index+1 == data.prizeDay) {
                    im_mask.active = (data.hasTake == 1);
                }

                im_flag_take.active = (index+1 < data.prizeDay);
            // }

            btn_reward.getComponent(cc.Button).enabled = ((index+1) != data.prizeDay);
            // imLight.active = (index+1 == data.prizeDay && data.hasTake != 1);

            if (index+1 == data.prizeDay) {
                im_flag_take.active = (data.hasTake == 1);
                btn_reward.getComponent(cc.Button).enabled = (data.hasTake == 0);
            }

        }
    }

    public clickDaily(event: cc.Event.EventTouch, customEventData: any) {
        console.log("click:"+customEventData)
        if (Number(customEventData) < this.prizeDay) {
            // cc.warn('没啥可领的啦，是在下输了');
            let popData = <TIP_INFO>{};
            popData.dur = 0.6;
            popData.type = 2;
            popData.tip = '当天登录奖励已领取过了,请明天再来';
            NotificationCenter.Instance.emit('pop_tips', popData);
            return;
        }
        if (Number(customEventData) > this.prizeDay) {
            // cc.warn('没啥可领的啦，是在下输了');
            let popData = <TIP_INFO>{};
            popData.dur = 0.6;
            popData.type = 2;
            popData.tip = '尚未开奖,请明天再来';
            NotificationCenter.Instance.emit('pop_tips', popData);
            return;
        }
        if (Number(customEventData) == this.prizeDay && this.hasTake == 1) {
            // cc.warn('没啥可领的啦，是在下输了');
            let popData = <TIP_INFO>{};
            popData.dur = 0.6;
            popData.type = 2;
            popData.tip = '当天登录奖励已领取过了,请明天再来';
            NotificationCenter.Instance.emit('pop_tips', popData);
            return;
        }

        let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
        let userInfo = logicCenter.getUserInfo();
        if (cc.sys.WECHAT_GAME == cc.sys.platform) {
            if (userInfo.adNum <= 0) {
                // cc.warn("今天视频次数已用完，每日24点重置");
                let popData = <TIP_INFO>{};
                popData.dur = 0.6;
                popData.type = 2;
                popData.tip = '今天视频次数已用完，每日24点重置';
                NotificationCenter.Instance.emit('pop_tips', popData);
                return;
            }
        }
        if (userInfo.adCD > 0) {
            let popData = <TIP_INFO>{};
            popData.dur = 0.6;
            popData.type = 2;
            popData.tip = '视频冷却中，还需要等待'+Math.ceil(userInfo.adCD)+'秒';
            NotificationCenter.Instance.emit('pop_tips', popData);
            return;
        }

        // NotificationCenter.Instance.emit("request7DayPrize");
        NotificationCenter.Instance.emit("requestAdGuide", dailyAdCode);

    }

    public takePrize(data: any) {
        this.hasTake = 1;
        let btn_reward = cc.find('im_bg/btn_reward'+this.prizeDay, this.node);
        btn_reward.getComponent(cc.Button).enabled = false;

        let im_flag_take = btn_reward.getChildByName('im_flag_take');
        im_flag_take.active = true;

        let im_mask = btn_reward.getChildByName('im_mask');
        im_mask.active = true;
    }

    public checkClose(event: cc.Touch) {
        let imBg = this.node.getChildByName('im_bg');
        let touchPos = imBg.convertToNodeSpaceAR(event.getLocation());
        cc.log("touch:"+touchPos);
        if (Math.abs(touchPos.x) > Math.abs(imBg.getContentSize().width/2) ||
            touchPos.y > imBg.getContentSize().height ||
            touchPos.y < 0) {
            this.node.active = false;
            this.node.destroy();
        }
    }

    public clickClose() {
        this.node.active = false;
        this.node.destroy();
    }
}
